// Copyright (C) 2010 Félix Cloutier.

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 2.0.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License 2.0 for more details.

// A copy of the GPL 2.0 should have been included with the program.
// If not, see http://www.gnu.org/licenses/

#include "JitBindings.h"

namespace LLVMJit
{

JitBindings::JitBindings(llvm::ExecutionEngine* engine, llvm::Module* module)
{
	this->engine = engine;
	this->module = module;
}

llvm::Function* JitBindings::GetBinding(void* address)
{
	if (bindings.count(address) == 0)
		return NULL;
	return bindings[address];
}

llvm::Function* JitBindings::GetBinding(JitBindings::NamedBinding bindingName)
{
	if (namedBindings.count(bindingName) == 0)
		return NULL;
	return namedBindings[bindingName];
}

void JitBindings::Bind(llvm::Function* llvmStub, void* address)
{
	engine->addGlobalMapping(llvmStub, address);
	bindings[address] = llvmStub;
}

void JitBindings::Bind(llvm::Function* llvmStub, void* address, JitBindings::NamedBinding bindingName)
{
	Bind(llvmStub, address);
	namedBindings[bindingName] = GetBinding(address);;
}

}